/*
Source : https://leetcode.com/problems/roman-to-integer/
Author : nflush@outlook.com
Date   : 2016-06-27
*/
/*
13. Roman to Integer
Total Accepted: 91425 Total Submissions: 225960 Difficulty: Easy

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

Subscribe to see which companies asked this question

*/

class Solution {
public:
    int romanToInt(string s) {
        int sum =0, pre = 1000, now = 0;
        for(int i = 0; i<s.size();i++){
            switch(s[i]){
            case 'M':now = 1000;break;
            case 'C':now = 100; break;
            case 'D':now = 500; break;
            case 'X':now = 10;  break;
            case 'L':now = 50;  break;
            case 'I':now = 1;   break;
            case 'V':now = 5;   break;
            }
            if (now > pre){
                sum += now -2 *pre;
            } else {
                sum += now;
            }
            pre = now;
        }
        return sum;
    }
};
